package com.javaweb.web.controller;

import java.util.Date;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.javaweb.annotation.token.TokenDataAnnotation;
import com.javaweb.annotation.url.ControllerMethod;
import com.javaweb.base.BaseResponseResult;
import com.javaweb.base.BaseService;
import com.javaweb.base.po.Config;
import com.javaweb.base.po.User;
import com.javaweb.constant.ApiCommonConstant.ConfigConstant;
import com.javaweb.constant.CommonConstant;
import com.javaweb.constant.SystemConstant.RedisConstant;
import com.javaweb.enums.AuthEnum;
import com.javaweb.enums.HttpCodeEnum;
import com.javaweb.util.entity.Page;
import com.javaweb.web.eo.TokenData;
import com.javaweb.web.eo.config.ConfigListRequest;

/**
 * 登录且需要权限才可访问的配置管理模块
 * <p>
 * 该模块主要提供配置管理相关的接口
 * 
 * @author tank2140896
 */
@RestController
@RequestMapping(ConfigConstant.WEB_CONFIG_PREFIX)
public class ConfigController extends BaseService {
    
	/**
	 * 新增配置接口
	 * 
	 * @param config 配置新增请求参数
	 * @param tokenData tokenData
	 * @return BaseResponseResult 接口通用返回参数
	 */
	@PostMapping(ConfigConstant.CONFIG_ADD)
	@ControllerMethod(interfaceName="新增配置接口",auth=AuthEnum.PERMISSION)
	public BaseResponseResult configAdd(@RequestBody @Validated Config config,@TokenDataAnnotation TokenData tokenData){
		User currentUser = tokenData.getUser();
		var date = new Date();
		config.setSort(CommonConstant.ZERO_STRING_VALUE);
		config.setUniversally(0);
		config.setCreator(currentUser.getUserId());
		config.setCreateDate(date);
		config.setUpdater(currentUser.getUserId());
		config.setUpdateDate(date);
		configService.configAdd(config);
		stringRedisTemplate.convertAndSend(RedisConstant.REDIS_CONFIG_TOPIC,RedisConstant.REDIS_CONFIG_TOPIC);
		return getBaseResponseResult(HttpCodeEnum.SUCCESS,"config.add.success");
	}
	
	/**
	 * 查询配置接口
	 * 
	 * @param configListRequest 配置列表请求参数
	 * @return BaseResponseResult 接口通用返回参数
	 */
	@PostMapping(ConfigConstant.CONFIG_LIST)
	@ControllerMethod(interfaceName="查询配置接口",auth=AuthEnum.PERMISSION)
	public BaseResponseResult configList(@RequestBody ConfigListRequest configListRequest){
		Page page = configService.configList(configListRequest);
		return getBaseResponseResult(HttpCodeEnum.SUCCESS,"config.list.success",page);
	}
	
	/**
	 * 修改配置接口
	 * 
	 * @param config 配置修改请求参数
	 * @param tokenData tokenData
	 * @return BaseResponseResult 接口通用返回参数
	 */
	@PutMapping(ConfigConstant.CONFIG_MODIFY)
	@ControllerMethod(interfaceName="修改配置接口",auth=AuthEnum.PERMISSION)
	public BaseResponseResult configModify(@RequestBody @Validated Config config,@TokenDataAnnotation TokenData tokenData){
		User currentUser = tokenData.getUser();
		config.setUpdater(currentUser.getUserId());
		config.setUpdateDate(new Date());
		configService.configModify(config);
		stringRedisTemplate.convertAndSend(RedisConstant.REDIS_CONFIG_TOPIC,RedisConstant.REDIS_CONFIG_TOPIC);
		return getBaseResponseResult(HttpCodeEnum.SUCCESS,"config.modify.success");
	}
	
	/**
	 * 配置详情接口
	 * 
	 * @param configId 配置ID
	 * @return BaseResponseResult 接口通用返回参数
	 */
	@GetMapping(ConfigConstant.CONFIG_DETAIL)
	@ControllerMethod(interfaceName="配置详情接口",auth=AuthEnum.PERMISSION)
	public BaseResponseResult configDetail(@PathVariable(name="configId",required=true) String configId){
		Config config = configService.configDetail(configId);
		return getBaseResponseResult(HttpCodeEnum.SUCCESS,"config.detail.success",config);
	}
	
	/**
	 * 删除配置接口（支持批量删除，用逗号隔开）
	 * 
	 * @param configId 配置ID
	 * @return BaseResponseResult 接口通用返回参数
	 */
	@DeleteMapping(ConfigConstant.CONFIG_DELETE)
	@ControllerMethod(interfaceName="删除配置接口（支持批量删除，用逗号隔开）",auth=AuthEnum.PERMISSION)
	public BaseResponseResult configDelete(@PathVariable(name="configId",required=true) String configId){
		configService.configDelete(configId);
		stringRedisTemplate.convertAndSend(RedisConstant.REDIS_CONFIG_TOPIC,RedisConstant.REDIS_CONFIG_TOPIC);
		return getBaseResponseResult(HttpCodeEnum.SUCCESS,"config.delete.success");
	}
	
}
